package dolf.objects.weapons.Kamikaze;

import javax.swing.ImageIcon;

import dolf.game.Level;
import dolf.game.Player;
import dolf.objects.Animation;
import dolf.objects.Ball;
import dolf.objects.ImageStore;
import dolf.objects.Pit;
import dolf.objects.Point;
import dolf.objects.Vector;
import dolf.objects.weapons.Weapon;
import dolf.window.SystemMessagesHandler;


public class Kamikaze implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 75;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Kamikaze.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Kamikaze.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Kamikaze.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Kamikaze/gfx/icon.png");
	
	private final int DEST_RAD = 35;
	private java.util.LinkedList<String> imgVec = new java.util.LinkedList<String>();
	private Player pl;
	private Animation explosion; 
	private boolean used;
	private boolean inititalized = false;
	
	
	public Kamikaze(){
		for (int i = 1; i <= 23; i++) {
			imgVec.add("dolf/gfx/environment/explosion/explosion"+i+".png");
		}
		explosion = new Animation(imgVec);
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.inititalized = true;
		this.pl = _pl;
		pl.setWeaponState();
		this.used = false;
	}
	
	public void deInitialize(){
		this.inititalized = false;
		this.pl = null;
	}
	
	public void execute() {
		if(Level.getInstance().mousePressed()&&pl.getBall().isMoving()&&!used){
			for(int i = 0; i < Level.getInstance().getPlayers().size();i++){
				Ball b = Level.getInstance().getPlayers().get(i).getBall();
				Player play = Level.getInstance().getPlayers().get(i);
				if(play == this.pl){
					continue;
				}
				double dist;
				if(b.isVisible() && (dist=this.pl.getBall().getPosition().getDist(b.getPosition()))<DEST_RAD){
					int destr = (int)(DEST_RAD-dist+30);
					if((int)(play.getHP()-destr)>0){
						this.pl.addDamage(destr);
						play.setHP(play.getHP()-destr);
						Vector tmp = b.getPosition().sub(this.pl.getBall().getPosition());
						b.setMotion(tmp.setLength(5));
					} else {
						play.setHP(play.getHP()-destr);
					}
				}
			}
			this.pl.addDamage(this.pl.getHP());
			this.pl.removeHP(this.pl.getHP());
			
			explosion.startAt(new Point(this.pl.getBall().getPosition().getIntX()-27, this.pl.getBall().getPosition().getIntY()-55), false);
						
			Level.getInstance().registerGround(new Pit(this.pl.getBall().getPosition().getCopy(),15, 0.001));
			this.remove();
		}
	}

	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Kamikaze/gfx/icon.png");	

	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}

	public int getPrice() {
		return Price;
	}
	public String getType(){
		return WeaponType;
	}

	public void remove() {
		pl.removeWeapon(this);
		this.pl.setAktWeapon(null);
	}
	public Player getPlayer(){
		return this.pl;
	}
	public double getState(){
			return 0;
	}
}
